package com.bbzn.device.client.controller;

import com.bbzn.device.client.vo.req.PostVO;
import com.bbzn.device.client.aspect.OperationAnnotation;
import com.bbzn.device.client.constant.CrudConstant;
import com.bbzn.device.client.constant.DeptConstant;
import com.bbzn.device.client.dataobject.Leader;
import com.bbzn.device.client.dataobject.Post;
import com.bbzn.device.client.dataobject.SystemParam;
import com.bbzn.device.client.exception.DefinitException;
import com.bbzn.device.client.service.CommonService;
import com.bbzn.device.client.service.LeaderService;
import com.bbzn.device.client.service.PostService;
import com.bbzn.device.client.service.SystemParamByCacheService;
import com.bbzn.device.client.utils.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Author wulongbo
 * @Date 2022/1/17 10:28
 * @Version 1.0
 */

@RestController
@RequestMapping("/job")
@Slf4j
public class PostController extends BaseApiService {

    @Autowired
    private CommonService commonService;

    @Autowired
    private SystemParamByCacheService systemParamByCacheService;

    @Autowired
    private PostService postService;

    @Autowired
    private LeaderService leaderService;


    /**
     * 添加职务
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/addPost", method = RequestMethod.POST)
    @OperationAnnotation(content = "添加职务")
    public BaseResponse addPost(@RequestBody PostVO postVO, HttpServletRequest request) throws Exception {
        Long userId = commonService.getUserId(request);
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        String username = MayiktJwtUtils.getUsername(request, secretKey);
//        Long userId = 1L;
        if (!OptionalBean.ofNullable(postVO.getName()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(DeptConstant.DEPT_ENUM, DeptConstant.POST_UN_NAME_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        Post insert = BabaBeanUtils.voToDto(postVO, Post.class);
        insert.setUserId(userId);
        insert.setCreateTime(new Date());
        insert.setCreatedBy(username);
        postService.insertSelective(insert);
        SystemParam systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.ADD_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 查询职务列表
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/getPostList", method = RequestMethod.POST)
    @OperationAnnotation(content = "查询职务列表")
    public BaseResponse getPostList(HttpServletRequest request) {
        Long userId = commonService.getUserId(request);
        Post query = new Post();
        query.setUserId(userId);
        List<Post> postList = postService.findByAll(query);
        return setResultSuccess(postList);
    }

    /**
     * 修改职务
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updatePost", method = RequestMethod.POST)
    @OperationAnnotation(content = "修改职务")
    public BaseResponse updatePost(@RequestBody PostVO postVO, HttpServletRequest request) throws Exception {
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        String username = MayiktJwtUtils.getUsername(request, secretKey);
        if (!OptionalBean.ofNullable(postVO.getId()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(DeptConstant.DEPT_ENUM, DeptConstant.POST_UN_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        Post update = BabaBeanUtils.voToDto(postVO, Post.class);
        update.setUpdatedBy(username);
        update.setUpdateTime(new Date());
        postService.updateByPrimaryKeySelective(update);
        SystemParam systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.MODIFY_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }

    /**
     * 删除职务
     *
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delPost", method = RequestMethod.POST)
    @OperationAnnotation(content = "删除职务")
    public BaseResponse delPost(@RequestBody PostVO postVO, HttpServletRequest request) throws Exception {
        String secretKey = commonService.getWebSalt(request);
        String language = MayiktJwtUtils.getLanguage(request, secretKey);
        if (!OptionalBean.ofNullable(postVO.getId()).isPresent()) {
            SystemParam systemParam = systemParamByCacheService.getValues(DeptConstant.DEPT_ENUM, DeptConstant.POST_UN_ID_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        Leader leader = new Leader();
        leader.setPostId(postVO.getId());
        List<Leader> leaders = leaderService.findByAll(leader);
        if(CollectionUtils.isNotEmpty(leaders)){
            SystemParam systemParam = systemParamByCacheService.getValues(DeptConstant.DEPT_ENUM, DeptConstant.POST_EXIST_POST_PREFIX + language);
            throw new DefinitException(systemParam.getCode(), systemParam.getMassage());
        }
        postService.deleteByPrimaryKey(postVO.getId());
        SystemParam systemParam = systemParamByCacheService.getValues(CrudConstant.CRUD_ENUM, CrudConstant.DELETE_SUCCESS_PREFIX + language);
        return setResultSuccess(systemParam.getMassage());
    }
}
